<?php

/*
 * Edit by xinli
 * Click nbfs://nbhost/SystemFileSystem/Templates/Scripting/PHPClass.php to edit this template
 */

namespace api\mall\controller;

use cmf\controller\RestBaseController;
use api\mall\model\InteriorConfigModel;
use think\db\Query;

/**
 * @OA\Tag(
 *     name="mall-interior",
 *     description="内景管理"
 * )
 */
class InteriorController extends RestBaseController
{
    protected function initialize()
    {
        parent::initialize();
        if (empty($this->user)) {
            $this->error('login');
        }
        online_record($this->userId);
    }

    /**
     * 获取内景资源列表
     * @OA\Get(
     *     description="回包说明 interior_type(1大屏管理 2图片管理) big_screen_type(1宣传图片 2宣传视频) big_screen_index(大屏索引 1一号大屏 2二号大屏 3三号大屏) picture_index(图片索引 1一号点位 2二号点位 3三号点位)",
     *     tags={"mall-interior"},
     *     path="/mall/interior/get_interior",
     *     @OA\Parameter(name="scene",in="query",description="内景场景（不传返全部） 1数藏商城 2美容院 3零售区",required=false,@OA\Schema(type="integer")),
     *     @OA\Response(response=200, ref="#/components/responses/200")
     * )
     */
    public function get_interior()
    {
        $param = $this->request->param();

        $interiorConfigModel = new InteriorConfigModel();
        $interiorQuery = $interiorConfigModel->alias('a');
        $interior = $interiorQuery        
            ->field('a.big_screen_type, a.big_screen_url, a.picture_url')
            ->where(function (Query $query) use ($param) {
                if (!empty($param['scene'])) {
                    $query->where('interior_scene', $param['scene']);
                }
            })
            ->order('big_screen_index', 'ASC')
            ->order('picture_index', 'ASC')
            ->paginate(20);

        foreach ($interior as $key => $value) {
            $interior[$key]['big_screen_url'] = cmf_get_image_url($value['big_screen_url']);
            $interior[$key]['picture_url'] = cmf_get_image_url($value['picture_url']);
        }

        $result = [];
        foreach ($interior as $key => $value) {
            if ($value['big_screen_type'] == 0) {
                $result[$key]['type'] = 1;
                $result[$key]['url'] = $value['picture_url'];
            } else if ($value['big_screen_type'] == 1) {
                $result[$key]['type'] = 1;
                $result[$key]['url'] = $value['big_screen_url'];
            } else {
                $result[$key]['type'] = 2;
                $result[$key]['url'] = $value['big_screen_url'];
            }
        }

        $this->success('ok', $result);
    }
}
